package org.geektimes.projects.user.service.impl;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.geekbang.projects.injection.Service;
import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.service.UserService;
import org.geektimes.projects.user.support.transaction.Transaction;
import org.hibernate.query.internal.NativeQueryImpl;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.servlet.annotation.WebListener;
import javax.validation.Valid;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 用户管理
 *
 * @author : lsq
 */
@Service
public class UserServiceImpl implements UserService {


	private static Logger logger = Logger.getLogger(UserServiceImpl.class.getName());


	/// TODO 循环依赖目前只做了校验，未做延迟注入实现
	@Resource(name = "jpa/entityManager")
	private EntityManager entityManager;


	@ConfigProperty(name = "application.name" , defaultValue = "default.name")
	private String applicationName;

	@ConfigProperty(name = "testNum" , defaultValue = "121")
	private Integer testNum;

	@PostConstruct
	public void init() {
		logger.log(Level.INFO, "初始化" + entityManager);
		logger.log(Level.INFO ,"applicationName :" + applicationName);
		logger.log(Level.INFO ,"testNum :" + testNum);
		System.out.println("初始化");
	}

	/**
	 * 获取所有用户
	 *
	 * @return
	 */
	@Override
	public List<User> queryAll() {
		return entityManager
				.createNativeQuery("select  * from users", User.class).unwrap(NativeQueryImpl.class)
				.getResultList();
	}


	/**
	 * 注册
	 *
	 * @param user 用户对象
	 * @return
	 */
	@Override
	@Transaction
	public boolean register(@Valid User user) {
		entityManager.persist(user);
		return true;

	}

	@Override
	public boolean deregister(User user) {
		return false;
	}

	@Override
	public boolean update(User user) {
		return false;
	}


	@Override
	public User queryUserById(Long id) {
		return null;
	}

	@Override
	public User queryUserByNameAndPassword(String name, String password) {
		return null;
	}
}
